package ru.compscicenter.uno.actor.bot;

import ru.compscicenter.uno.log.Logger;
import ru.compscicenter.uno.actor.Actor;
import ru.compscicenter.uno.controller.AController;
import ru.compscicenter.uno.model.Card;
import ru.compscicenter.uno.model.ModelException;
import ru.compscicenter.uno.model.Player;

import java.util.Random;


/**
 * User: SLeeP
 * Date: 03.12.11
 * Time: 11:00
 */
public class Bot extends Thread implements Actor {
    private static final String COLOR_BLUE = "blue";
    private static final String COLOR_GREEN = "green";
    private static final String COLOR_RED = "red";
    private static final String COLOR_YELLOW = "yellow";


    AController controller;
    public static String lastOperation;
    public boolean isChanged = false;
    public boolean takenCard = false;
    public boolean droppedCard = false;
    private int lastDroppedCard = -1;

    public AController getController() {
        return controller;
    }

    public void setController(AController controller) {
        this.controller = controller;
    }


    public void setDroppedCard(boolean droppedCard) {
        this.droppedCard = droppedCard;
    }

    public Bot(AController controller) {
        this.controller = controller;
    }

    public String getAction() {
        //System.out.println("ID = " + controller.getMyPlayerId());
        try {
            Thread.sleep(10);
          //  System.out.println("0");
        } catch (InterruptedException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        //System.out.println("1");
        if (droppedCard) {
            droppedCard = false;
            lastDroppedCard = -1;
        }

        //System.out.println("2");
        lastDroppedCard++;
        if (lastDroppedCard < controller.getPlayer(controller.getMyPlayerId()).getCards().size()) {
         //   System.out.println("3");
            Card  card = controller.getPlayer(controller.getMyPlayerId()).getCards().get(lastDroppedCard);
            //Logger.write("Bot " + controller.getMyPlayerId() + " trying to drop card " + card.getId(), Logger.Mode.ACTION);
            return "drop " + card.getId();
        }

        if (!takenCard) {
          //  System.out.println("4");
            takenCard = true;
            lastDroppedCard = -1;
 //           System.out.println("*******************************take");
            //Logger.write("Bot " + controller.getMyPlayerId() + " trying to take card ", Logger.Mode.ACTION);
            return "take";
        } else {
           // System.out.println("5");
            takenCard = false;
            droppedCard = false;
            lastDroppedCard = -1;
   //         System.out.println("*******************************end turn");
            //Logger.write("Bot " + controller.getMyPlayerId() + " trying to finish turn ", Logger.Mode.ACTION);
            return "end turn";
        }

        /*
        while (true) {
            boolean succeed = false;

            while (controller.getCurrentPlayerId() != controller.getMyPlayerId()) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            succeed = tryToDropAny();

            if (succeed) {
                controller.doEndTurn(controller.getMyPlayerId());
            } else {
                if (controller.doTakeCard(controller.getMyPlayerId())) {
                    tryToDropAny();
                    if (lastOperation == null) {
                        lastOperation = "DO_TAKE_CARD";
                    }
                }
                controller.doEndTurn(controller.getMyPlayerId());
            }

            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        } */
    }

    private boolean tryToDropAny() {
        boolean succeed = false;

        for (Card card : controller.getPlayer(controller.getMyPlayerId()).getCards()) {
            if (!card.getColor().equals(Card.Color.ANY)){
                try {
                    succeed = controller.doDropCard(controller.getMyPlayerId(), card.getId());
                } catch (ModelException e) {
                    e.printStackTrace();    //todo: what to do?
                }
                if (succeed) {
                    lastOperation = "DO_DROP_CARD " +  controller.getMyPlayerId() +" " + card.getId();
                    break;
                }
            } else {
                try {
                    succeed = controller.doDropCard(controller.getMyPlayerId(), card.getId(), Card.Color.YELLOW);
                } catch (ModelException e) {
                    e.printStackTrace();  //todo: what to do?
                }
                if (succeed) {
                    lastOperation = "DO_DROP_CARD " +  controller.getMyPlayerId() +" " + card.getId() + " " + Card.Color.YELLOW;
                    break;
                }
            }
        }
        return succeed;
    }


    public synchronized void printPlayersState() {
		System.out.println("Players:");
		for (Player player : controller.getPlayers()) {
			System.out.println(player);
		}
	}
    
    public String getColor() {
        Random rand = new Random();
        int a = rand.nextInt(100) % 4;

        switch (a) {
            case 0: return "yellow";
            case 1: return "blue";
            case 2: return "red";
            case 3: return "green";
            default: return "yellow";
        }
    }
}
